package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IPersonHistoryDao;
import com.boss.salary.entity.PersonHistory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Repository
public class PersonHistoryDaoImpl extends BaseDaoImpl implements IPersonHistoryDao {

    @Override
    public void batchSavePersonHistory(List<PersonHistory> list) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        StringBuilder sb = new StringBuilder();
        List<Object> params = new ArrayList<>();
        Class<?> clazz = PersonHistory.class;
        sb.append(" insert into ")
                .append(TableName.PERSON_HISTORY)
                .append(" values ");
        for(PersonHistory history : list){
            sb.append(" ( ");
            Field[] fields = clazz.getDeclaredFields();
            for(int i = 0; i < fields.length; i++){
                sb.append(" ? ");
                String columnName = fields[i].getName();
                Method getMethod = clazz.getDeclaredMethod("get" + columnName.substring(0,1).toUpperCase() + columnName.substring(1));
                params.add(getMethod.invoke(history));
                if(i != fields.length - 1){
                    sb.append(" , ");
                }
            }
            sb.append(" ) ");
            if(list.indexOf(history) != list.size() - 1){
                sb.append(" , ");
            }
        }

        super.getjdbcTemplate().update(sb.toString(), params.toArray());
    }

    @Override
    public PersonHistory selectHistoryPersonExtByPersonId(String personId, String salaryMonth) {
        StringBuilder sb = new StringBuilder();
        sb.append(" select ")
                .append(" person_address, dept_id, enter_agency_date, retire_date, ")
                .append(" person_type_code, person_ide_code, end_date, dead_date, ")
                .append(" remarks, sala_card_no, sala_card_bank, gov_cred_card_no, ")
                .append(" gov_cred_card_bank, start_date, merit_pay_code, sch_rec_code, ")
                .append(" pos_code, gr_code, tec_gr_code, is_auth, ")
                .append(" sala_gr_code, person_state, change_state, per_source_code, ")
                .append(" is_special_post, is_insurance, is_uni_sala,person_name,iden_no,state,phone,func_id,func_name,func_code ")
                .append(" from ").append(TableName.PERSON_HISTORY)
                .append(" where ").append(" is_deleted = 0 ")
                .append(" and salary_month = ? and person_id = ? ");
        Object[] params = {
                salaryMonth, personId
        };
        try{
            return super.getjdbcTemplate().queryForObject(sb.toString(), params, new BeanPropertyRowMapper<>(PersonHistory.class));
        }catch(EmptyResultDataAccessException e){
            return null;
        }
    }
}
